<template>
  <div class="test-chat">
    <el-card>
      <template #header>
        <h2>聊天测试页面</h2>
      </template>
      
      <div class="test-info">
        <p><strong>当前消息数量:</strong> {{ chatMessages.length }}</p>
        <p><strong>测试模式:</strong> {{ isTestMode ? '是' : '否' }}</p>
        <p><strong>用户信息:</strong> {{ userInfo }}</p>
      </div>
      
      <div class="test-actions">
        <el-button @click="addTestMessage('user')">添加用户消息</el-button>
        <el-button @click="addTestMessage('assistant')">添加AI消息</el-button>
        <el-button @click="clearMessages">清空消息</el-button>
        <el-button @click="testAPI">测试API</el-button>
      </div>
      
      <div class="chat-display">
        <h3>消息列表:</h3>
        <div class="message-list">
          <div 
            v-for="(message, index) in chatMessages" 
            :key="index"
            :class="['message', message.type]"
          >
            <div class="message-content">
              <el-avatar :size="32" :icon="message.type === 'user' ? 'UserFilled' : 'Service'" />
              <div class="message-text">{{ message.content }}</div>
            </div>
            <div class="message-time">{{ formatTime(message.timestamp) }}</div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useAuthStore } from '@/stores/auth'
import api from '@/api'
import { ElMessage } from 'element-plus'

const authStore = useAuthStore()

// 聊天消息
const chatMessages = ref([
  {
    type: 'assistant',
    content: '你好！我是AI编程助手，有什么可以帮助你的吗？',
    timestamp: new Date()
  }
])

// 计算属性
const isTestMode = computed(() => localStorage.getItem('testMode') === 'true')
const userInfo = computed(() => authStore.user ? JSON.stringify(authStore.user) : '未登录')

// 添加测试消息
const addTestMessage = (type) => {
  const message = {
    type,
    content: type === 'user' ? '这是一条测试用户消息' : '这是一条测试AI消息',
    timestamp: new Date()
  }
  chatMessages.value.push(message)
  ElMessage.success(`添加了${type === 'user' ? '用户' : 'AI'}消息`)
}

// 清空消息
const clearMessages = () => {
  chatMessages.value = []
  ElMessage.success('消息已清空')
}

// 测试API
const testAPI = async () => {
  try {
    const response = await api.post('/assistant/chat', {
      message: '测试消息',
      context: { user_level: 'beginner' }
    })
    ElMessage.success('API测试成功')
    console.log('API响应:', response)
  } catch (error) {
    ElMessage.error('API测试失败')
    console.error('API错误:', error)
  }
}

// 格式化时间
const formatTime = (timestamp) => {
  return new Date(timestamp).toLocaleTimeString()
}
</script>

<style scoped>
.test-chat {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
}

.test-info {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.test-actions {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.chat-display {
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  padding: 15px;
  max-height: 400px;
  overflow-y: auto;
}

.message-list {
  margin-top: 10px;
}

.message {
  margin-bottom: 15px;
}

.message.user {
  display: flex;
  justify-content: flex-end;
}

.message.assistant {
  display: flex;
  justify-content: flex-start;
}

.message-content {
  display: flex;
  gap: 10px;
  max-width: 70%;
  align-items: flex-start;
}

.message.user .message-content {
  flex-direction: row-reverse;
}

.message.assistant .message-content {
  flex-direction: row;
}

.message-text {
  padding: 10px 15px;
  border-radius: 10px;
  word-wrap: break-word;
  max-width: calc(100% - 50px);
}

.message.user .message-text {
  background-color: #409eff;
  color: white;
}

.message.assistant .message-text {
  background-color: #f5f7fa;
  color: #333;
}

.message-time {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
  text-align: right;
}
</style>
